package com.tianji.learning.mq;

import com.tianji.api.dto.msg.LikedTimesDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class LikedListener {
    private final IInteractionReplyService interactionReplyService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "qa.liked.times.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = "QA.times.changed"
    ))
    public void likedTimesChanged(List<LikedTimesDTO> List) {
        log.info("收到点赞次数变更消息,{}", List);
        for (LikedTimesDTO likedTimesDTO : List) {
            if (likedTimesDTO != null) {
                //去更新数据库
                interactionReplyService.lambdaUpdate()
                        .eq(likedTimesDTO.getBizId() != null,
                                InteractionReply::getId, likedTimesDTO.getBizId())
                        .set(InteractionReply::getLikedTimes, likedTimesDTO.getLikedTimes());
            }
        }
    }

}
